home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / odlist / odlist.frm < prev    next >
Text File  |  1994-12-06  |  12KB  |  342 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "VB Messenger Owner-Draw ListBox Sample"
  5.    ClientHeight    =   5415
  6.    ClientLeft      =   6135
  7.    ClientTop       =   2970
  8.    ClientWidth     =   7755
  9.    Height          =   5820
  10.    Left            =   6075
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    ScaleHeight     =   5415
  14.    ScaleWidth      =   7755
  15.    Top             =   2625
  16.    Width           =   7875
  17.    Begin ListBox List1 
  18.       Height          =   2370
  19.       Left            =   180
  20.       TabIndex        =   8
  21.       Top             =   2430
  22.       Width           =   3495
  23.    End
  24.    Begin PictureBox picFile 
  25.       AutoRedraw      =   -1  'True
  26.       AutoSize        =   -1  'True
  27.       BorderStyle     =   0  'None
  28.       Height          =   195
  29.       Left            =   3300
  30.       Picture         =   ODLIST.FRX:0000
  31.       ScaleHeight     =   195
  32.       ScaleWidth      =   195
  33.       TabIndex        =   7
  34.       Top             =   5970
  35.       Width           =   195
  36.    End
  37.    Begin PictureBox picDir 
  38.       AutoRedraw      =   -1  'True
  39.       AutoSize        =   -1  'True
  40.       BorderStyle     =   0  'None
  41.       Height          =   195
  42.       Left            =   3060
  43.       Picture         =   ODLIST.FRX:00E2
  44.       ScaleHeight     =   195
  45.       ScaleWidth      =   195
  46.       TabIndex        =   6
  47.       Top             =   5970
  48.       Width           =   195
  49.    End
  50.    Begin CommandButton Command1 
  51.       Cancel          =   -1  'True
  52.       Caption         =   "End Demo"
  53.       Default         =   -1  'True
  54.       Height          =   435
  55.       Left            =   2970
  56.       TabIndex        =   1
  57.       Top             =   4890
  58.       Width           =   1575
  59.    End
  60.    Begin VBMsg VBMsg1 
  61.       Height          =   420
  62.       Left            =   7140
  63.       MessageCount    =   ODLIST.FRX:01C4
  64.       MessageList     =   ODLIST.FRX:01C6
  65.       MessageTypes    =   0  'Selected Messages
  66.       PostDefault     =   0   'False
  67.       Top             =   4830
  68.       Width           =   420
  69.    End
  70.    Begin Frame Frame1 
  71.       Caption         =   "Description"
  72.       Height          =   4665
  73.       Left            =   3810
  74.       TabIndex        =   2
  75.       Top             =   120
  76.       Width           =   3795
  77.       Begin Label Label3 
  78.          BackStyle       =   0  'Transparent
  79.          Caption         =   "This sample program uses VB Messenger to trap the WM_DRAWITEM message that gets sent to the form (the parent of the list box) and draws a picture with text for each item.  This sample also uses the Windows API extensively."
  80.          FontBold        =   0   'False
  81.          FontItalic      =   0   'False
  82.          FontName        =   "MS Sans Serif"
  83.          FontSize        =   8.25
  84.          FontStrikethru  =   0   'False
  85.          FontUnderline   =   0   'False
  86.          ForeColor       =   &H00FF0000&
  87.          Height          =   1035
  88.          Left            =   180
  89.          TabIndex        =   5
  90.          Top             =   3450
  91.          Width           =   3435
  92.       End
  93.       Begin Label Label2 
  94.          BackStyle       =   0  'Transparent
  95.          Caption         =   "When an item in the list box needs to be drawn (i.e., because of a repaint or if focus or the selection has changed), Windows sends the parent control (or form) a WM_DRAWITEM message.  With that message comes a pointer to a data structure (or Type) that contains all the information needed to draw the item (such as the rectangle in which to draw it)"
  96.          FontBold        =   0   'False
  97.          FontItalic      =   0   'False
  98.          FontName        =   "MS Sans Serif"
  99.          FontSize        =   8.25
  100.          FontStrikethru  =   0   'False
  101.          FontUnderline   =   0   'False
  102.          ForeColor       =   &H00FF0000&
  103.          Height          =   1665
  104.          Left            =   180
  105.          TabIndex        =   4
  106.          Top             =   1680
  107.          Width           =   3435
  108.       End
  109.       Begin Label Label1 
  110.          BackStyle       =   0  'Transparent
  111.          Caption         =   "This sample program uses VB Messenger to display an owner-draw list box.  Owner-draw means that all drawing of text and/or pictures that appear in the list box is drawn by the programmer, rather than automatically handled by the list box itself."
  112.          FontBold        =   0   'False
  113.          FontItalic      =   0   'False
  114.          FontName        =   "MS Sans Serif"
  115.          FontSize        =   8.25
  116.          FontStrikethru  =   0   'False
  117.          FontUnderline   =   0   'False
  118.          ForeColor       =   &H00FF0000&
  119.          Height          =   1170
  120.          Left            =   180
  121.          TabIndex        =   3
  122.          Top             =   360
  123.          Width           =   3435
  124.          WordWrap        =   -1  'True
  125.       End
  126.    End
  127.    Begin ListBox lstOwnerDraw 
  128.       Height          =   2175
  129.       Left            =   180
  130.       TabIndex        =   0
  131.       Top             =   210
  132.       Width           =   3495
  133.    End
  134. End
  135. Option Explicit
  136.  
  137. Sub Command1_Click ()
  138.     End
  139. End Sub
  140.  
  141. Sub DrawItem (lpdis As DRAWITEMSTRUCT)
  142.  
  143.     Dim rc&
  144.     Dim lpstr$
  145.     Dim hdcSource%, cx%, cy%
  146.     Dim cSelBack&
  147.     
  148.     'If no items in list box yet, indicate focus for
  149.     'specified rectangle
  150.     If (lpdis.itemID = -1) Then
  151.     DrawFocusRect lpdis.hDC, lpdis.rcItem
  152.     Exit Sub
  153.     End If
  154.  
  155.     'If Windows wants us to draw the entire item of just change
  156.     'the selection state, we do this stuff
  157.     If (lpdis.itemAction And ODA_DRAWENTIRE) Or (lpdis.itemAction And ODA_SELECT) Then
  158.     
  159.     'If the item it is selected, fill in the rectangle
  160.     'with the system color for highlight. If not selected,
  161.     'fill the rectangle with the standard window color.
  162.     'Also, the the background and foreground colors
  163.     'appropriately based on selection.
  164.     If (lpdis.itemState And ODS_SELECTED) Then
  165.         cSelBack = GetSysColor(COLOR_HIGHLIGHT)
  166.         rc = SetBkColor(lpdis.hDC, cSelBack)
  167.         rc = SetTextColor(lpdis.hDC, GetSysColor(COLOR_HIGHLIGHTTEXT))
  168.         DrawSelectionRect lpdis, cSelBack
  169.     Else
  170.         cSelBack = GetSysColor(COLOR_WINDOW)
  171.         rc = SetBkColor(lpdis.hDC, cSelBack)
  172.         rc = SetTextColor(lpdis.hDC, GetSysColor(COLOR_WINDOWTEXT))
  173.         DrawSelectionRect lpdis, cSelBack
  174.     End If
  175.     
  176.     'If the item is a directory, use the picDir picture
  177.     'else use the picFile picture.  The hDC property
  178.     'is used for drawing with the Windows API as we will
  179.     'do next.
  180.     If (lstOwnerDraw.ItemData(lpdis.itemID)) Then
  181.         hdcSource = picDir.hDC
  182.     Else
  183.         hdcSource = picFile.hDC
  184.     End If
  185.     'All Windows API calls require that coordinates and sizes
  186.     'be in pixels.
  187.     cx = picDir.Width / Screen.TwipsPerPixelX
  188.     cy = picDir.Height / Screen.TwipsPerPixelY
  189.     'This function copies the image in the picture box
  190.     'to an area specified
  191.     rc = BitBlt(lpdis.hDC, lpdis.rcItem.left, lpdis.rcItem.top, cx, cy, hdcSource, 0, 0, SRCCOPY)
  192.     
  193.     'Now, draw the text using the DrawText Windows API
  194.     lpdis.rcItem.left = lpdis.rcItem.left + cx + 5
  195.     lpstr = lstOwnerDraw.List(lpdis.itemID)
  196.     rc = DrawText(lpdis.hDC, lpstr, Len(lpstr), lpdis.rcItem, DT_VCENTER Or DT_SINGLELINE)
  197.         
  198.     'if item has focus, do additional drawing -- dashed border
  199.     If (lpdis.itemState And ODS_FOCUS) Then
  200.         DrawFocusRect lpdis.hDC, lpdis.rcItem
  201.     End If
  202.  
  203.     Exit Sub
  204.  
  205.     End If
  206.  
  207.     'If only focus has changed, display or hide the focus rectangle.
  208.     'DrawFocusRect will display the dotted rectangle if one isn't
  209.     'already there, otherwise if there is one it will clear it.
  210.     If (lpdis.itemAction And ODA_FOCUS) Then
  211.     DrawFocusRect lpdis.hDC, lpdis.rcItem
  212.     Exit Sub
  213.     End If
  214.     
  215. End Sub
  216.  
  217. '
  218. 'This routine fills an area (a rectangle) with a color
  219. '
  220. Sub DrawSelectionRect (lpdis As DRAWITEMSTRUCT, cSelBack As Long)
  221.  
  222.     Dim rc&
  223.     Dim hbrSel%, hbrOld%
  224.     
  225.     hbrSel = CreateSolidBrush(cSelBack)
  226.     hbrOld = SelectObject(lpdis.hDC, hbrSel)
  227.     rc = FillRect(lpdis.hDC, lpdis.rcItem, hbrSel)
  228.     rc = SelectObject(lpdis.hDC, hbrOld)
  229.     rc = DeleteOb